#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2020/7/16 16:42
# @Author : LiShan
# @File : imageCollect.py
# @Software: PyCharm
# @description：通过左右两个相机拍照采集图像并保存到指定目录（用于相机标定前的采集图像工作）
import cv2
import os

cv2.namedWindow("left", 0)
cv2.namedWindow("right", 0)
cv2.moveWindow("left", 0, 0)
cv2.moveWindow("right", 801, 0)
cv2.resizeWindow("left", 800, 600)
cv2.resizeWindow("right", 800, 600)
left_camera = cv2.VideoCapture(0)
right_camera = cv2.VideoCapture(1)

# 设置左右摄像头的分辨率
left_camera.set(3, 800)
left_camera.set(4, 600)
right_camera.set(3, 800)
right_camera.set(4, 600)

# 拍照次数
counter = 1

# 棋盘格尺寸
pattern = (7, 5)

# 拍照文件目录
folder = './img/'
if os.path.exists(folder):
    pass
else:
    os.makedirs(folder)


def shot(pos, frame):
    global counter
    path = folder + pos + "_" + str(counter) + ".bmp"
    cv2.imwrite(path, frame)
    print("snapshot saved into: " + path)


while True:
    ret1, left_frame = left_camera.read()
    ret2, right_frame = right_camera.read()
    cv2.imshow("left", left_frame)
    cv2.imshow("right", right_frame)

    key = cv2.waitKey(1)
    if key == ord('q'):
        break
    # 按下s键，拍摄当前图像
    elif key == ord('s'):
        shot("left", left_frame)
        shot("right", right_frame)
        counter += 1

left_camera.release()
right_camera.release()
